package com.kevin.design.creationalPattern.simpleFactory;

import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.swing.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;

public class SimpleFactoryOfChartByXML {
    public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
        SimpleFactoryXML simpleFactoryXML1 = SimpleFactoryXML.getInstance();
        SimpleFactoryXML simpleFactoryXML2 = SimpleFactoryXML.getInstance();
        System.out.println(simpleFactoryXML1==simpleFactoryXML2?true:false);

        String chartType = XMLUtil.getChartType();
        ChartXML chart = simpleFactoryXML1.getChart(chartType);
        chart.display();
    }
}

class XMLUtil{
    public static String getChartType() throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new File("src/main/java/com\\kevin\\design\\creationalPattern\\simpleFactory\\config.xml"));
        NodeList nodeList = document.getElementsByTagName("chartType");
        Node doc = nodeList.item(0).getFirstChild();
        String chartType = doc.getNodeValue().trim();
        return chartType;

    }
}

interface ChartXML{
    abstract void display();
}

class SimpleFactoryXML{
    private SimpleFactoryXML() {
        System.out.println("创建工厂");
    }

    private static class Instance{
        private final static SimpleFactoryXML instance = new SimpleFactoryXML();
    }

    public static SimpleFactoryXML getInstance(){
        return Instance.instance;
    }

    public ChartXML getChart(String chartName){
        ChartXML chart = null;
        if(chartName.equals("柱状图")){
//            chart = new HistogramChart();
        }else if (chartName.equals("饼状图")){
//            chart = new PieChart();
        }else if (chartName.equals("折线图")){
            chart = new LineChartXML();
        }
        return chart;
    }
}

class LineChartXML implements ChartXML {



    public LineChartXML() {
        System.out.println("创建折线图");
    }

    @Override
    public void display() {
        System.out.println("显示折线图");
    }
}